//
//  CreateGroupActiveViewController.m
//  Findif
//
//  Created by Mac on 2019/3/6.
//  Copyright © 2019年 Cicel Chen. All rights reserved.
//

#import "CreateGroupActiveViewController.h"
#import "ActiveHeaderView.h"
#import "HomeTableViewCell.h"
#import "GoodsModel.h"
#import "GoodsDetailsViewController.h"

@interface CreateGroupActiveViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, strong)NSArray<GoodsModel *> * goodlistArray;

@end

@implementation CreateGroupActiveViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeGoodsListData:) name:@"changeGoodsListData" object:nil];
    
    self.title = @"商品列表";
    [self loadData];
    [self setupUI];
}


- (void)loadData{
    
    [[ZRNetworkTool sharedTool] getGoodsListResultWithCategory:@"" WithIs_merchants_get:@"0" WithBlock:^(BOOL success, id responseData) {
        
        if (success) {
            
            self.goodlistArray = [NSArray yy_modelArrayWithClass:[GoodsModel class] json:responseData[@"data"]];
        }
    }];
}


- (void)setGoodlistArray:(NSArray<GoodsModel *> *)goodlistArray{
    
    _goodlistArray = goodlistArray;
    
    [self.tableView reloadData];
}


- (void)setupUI{

    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = ColorWithRGB(0xF8F9FA);
    tableView.rowHeight = 120 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.showsVerticalScrollIndicator = NO;
    //    self.tableView.bounces = NO;
    //    self.tableView.estimatedRowHeight = 150 * SCREENH6;//估算高度
    //    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    self.tableView = tableView;
    
    [self.tableView registerClass:[HomeTableViewCell class] forCellReuseIdentifier:@"CreatGroupGoodsListCell"];
    [self.tableView registerClass:[ActiveHeaderView class] forHeaderFooterViewReuseIdentifier:@"CreatGroupHeaderView"];
    
//    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//
//        [self loadData];
//    }];
    
    [self.view addSubview:self.tableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.goodlistArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CreatGroupGoodsListCell" forIndexPath:indexPath];
    
    cell.model = self.goodlistArray[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 95 * SCREENH6;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    ActiveHeaderView * header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"CreatGroupHeaderView"];
    
    return header;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HiddenBar;
    
    GoodsModel * model = self.goodlistArray[indexPath.row];
    GoodsDetailsViewController * detailVC = [GoodsDetailsViewController new];
    detailVC.ID = model.id;
    detailVC.infoType = [NSString stringWithFormat:@"%@",model.merchants_goods_type];
    detailVC.state = @"create";
    detailVC.group_id = self.group_id;
    
    [self.navigationController pushViewController:detailVC animated:YES];
    
    ShowBar;
}


- (void)changeGoodsListData:(NSNotification *)noti{
    
    
    NSString * ID = noti.userInfo[@"ID"];
    NSString * type = noti.userInfo[@"type"];
    if ([type isEqualToString:@"first"]) {
        
    }else{
        
        self.goodlistArray = nil;
    }

    [[ZRNetworkTool sharedTool] getGoodsListResultWithCategory:ID WithIs_merchants_get:@"0" WithBlock:^(BOOL success, id responseData) {
        
        if (success) {
            
            self.goodlistArray = [NSArray yy_modelArrayWithClass:[GoodsModel class] json:responseData[@"data"]];
        }
    }];
}



@end
